package com.xwj.back.controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.xwj.back.pojo.Notice;
import com.xwj.back.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/notice/getAll")
    public JSONObject getNotices(@RequestParam Integer page){
        return noticeService.getNotices(page);
    }

    @GetMapping("/main/sendAll/")
    public List<Notice> getSends(){
        return noticeService.getSendAll();
    }
    @GetMapping("/notice/add/")
    public Map<String,String> addNotice(Notice notice){
        Map<String,String> map = new HashMap<>();
        map.put("message",noticeService.addNotice(notice));
        return map;
    }

    @PostMapping("/notice/delete/")
    public Map<String,String> deleteNotice(@RequestBody Integer id){
        Map<String,String> map = new HashMap<>();
        map.put("message",noticeService.deleteNotice(id));
        return map;
    }

    @PostMapping("/notice/update/")
    public Map<String,String> updateNotice(@RequestBody Map<String,String> data){
        Map<String,String> map = new HashMap<>();
        map.put("message",noticeService.updateNotice(data));
        return map;
    }

    @GetMapping("/notice/send/")
    public Map<String,String> sendNotice(@RequestParam Integer id){
        Map<String,String> map = new HashMap<>();
        map.put("message",noticeService.sendNotice(id));
        return map;
    }

    @GetMapping("/notice/typeLike/")
    public List<Notice> getLike(@RequestParam Map<String,String> data){
        String type = data.get("type");
        boolean send = Boolean.parseBoolean(data.get("send"));
        return noticeService.getLike(type,send);
    }

    //批量删除
    @PostMapping("/notice/bulkDelete/")
    public Map<String,String> bulkDelete(@RequestBody JSONObject req){
        HashMap<String, String> map = new HashMap<>();
        List<Notice> notices = req.getObject("notices", new TypeReference<List<Notice>>(){});
        map.put("message",noticeService.bulkDelete(notices));
        return map;
    }
}
